package io.gitee.kotle.android.mvvm.mvvminterface

import android.content.res.Resources
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleOwner
import io.gitee.kotle.android.utils.ActivityResultHelper
import io.gitee.kotle.android.utils.isPadDevice
import io.gitee.kotle.android.utils.isScreenPortrait

interface IBaseMvvm : IOnSingleClickListener {
    /**
     * 创建之前的回调
     */
    fun beforeMvvmCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): ViewGroup? {
        return null
    }

    /**
     * 创建展示的ui
     */
    fun onMvvmCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): MvvmViewType?

    /**
     * 最后处理创建的ui
     */
    fun afterMvvmCreateView(
        inflater: LayoutInflater,
        beforeView: ViewGroup?,
        createView: MvvmViewType?,
        savedInstanceState: Bundle?
    ): MvvmViewType? {
        if (beforeView != null) {
            val child = when (createView) {
                is MvvmViewType.LayoutResType -> {
                    inflater.inflate(createView.layoutRes, beforeView, false)
                }
                is MvvmViewType.ViewBindingType -> {
                    createView.binding.root
                }
                is MvvmViewType.ViewInstanceType -> {
                    createView.view
                }
                else -> null
            }
            if (child != null && child.parent == null) {
                beforeView.addView(
                    child, child.layoutParams ?: ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                )
            }
            return MvvmViewType.ViewInstanceType(beforeView)
        }
        return createView
    }

    /**
     * 获取需要注册点击的view
     */
    fun getClickView(): List<View?>? = null

    /**
     *  规范fragment和activity的回调
     *  开始处理和view相关的工作，在[getClickView]之后调用
     */
    fun initUi(savedInstanceState: Bundle?) {}

    /**
     * 注册livedata
     */
    fun initObserverLiveData() {}

    /**
     * 规范fragment和activity的回调
     * 开始处理和数据相关的工作，在[initUi]之后调用
     */
    fun initData() {}


    /**
     * 竖屏回调
     */
    fun onScreenOrientationPortrait() {}

    /**
     * 横屏回调
     */
    fun onScreenOrientationLandscape() {}

    /**
     * 屏幕改变
     */
    fun onScreenOrientationChange(isPortrait: Boolean) {}


    /**
     * 默认的view创建逻辑
     * 1。先回调[beforeMvvmCreateView]
     * 2.拿第一步的结果作为容器，如果是null，则使用传入的容器，然后回调[onMvvmCreateView]
     * 3.对第一步和第二步的view，做判断，返回最终的view
     */
    fun onHandleCreateMvvmViewType(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val beforeView = beforeMvvmCreateView(inflater, container, savedInstanceState)
        val realContainer = beforeView ?: container
        val createView = onMvvmCreateView(inflater, realContainer, savedInstanceState)
        return when (val viewType =
            afterMvvmCreateView(inflater, beforeView, createView, savedInstanceState)) {
            is MvvmViewType.LayoutResType -> {
                inflater.inflate(viewType.layoutRes, realContainer, false)
            }
            is MvvmViewType.ViewBindingType -> {
                viewType.binding.root
            }
            is MvvmViewType.ViewInstanceType -> {
                viewType.view
            }
            null -> {
                null
            }
        }
    }

    /**
     * 当view创建完毕
     * 如果不清楚逻辑，建议子类慎重重写
     */
    fun onViewCreatedFinish(
        owner: LifecycleOwner,
        savedInstanceState: Bundle?,
        resources: Resources
    ) {
        //注册需要点击的view
        getClickView()?.forEach {
            it?.setOnClickListener(this)
        }
        //回调initUi
        initUi(savedInstanceState)
        //检查是否是横竖屏
        checkScreenPortrait(resources)
        //注册liveDate
        initObserverLiveData()
        //回调initData
        initData()
    }

    fun checkScreenPortrait(resources: Resources)
}